/*
 * PeriodicTableAcess.java
 * 
 * Please try not to modify this file unless it's extremly necesary or you know
 * what you are doing. Support for this file by me is limited, since i don't
 * have much knowledge on reflection
 * 
 * Searches for an element and gets it's information.
 * 
 * The program uses reflection witch is very sensitive thats why i have the
 * arrays. This avoids any comon mistakes.
 * 
 * Author: Andres Ruiz 
 * License: see License.txt
 */
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PeriodicTableAcess
{
	// Element Names Organized in Acending Atomic Number
	private final String[] ElementNames = { "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon",
			"Nitrogen", "Oxygen", "Fluorine", "Neon", "Sodium", "Magnesium", "Aluminium", "Silicon", "Phosphorus",
			"Sulfur", "Chlorine", "Argon", "Potassium", "Calcium", "Scandium", "Titanium", "Vanadium", "Chromium",
			"Manganese", "Iron", "Cobalt", "Nickel", "Copper", "Zinc", "Gallium", "Germanium", "Arsenic", "Selenium",
			"Bromine", "Krypton", "Rubidium", "Strontium", "Yttrium", "Zirconium", "Niobium", "Molybdenum",
			"Technetium", "Ruthenium", "Rhodium", "Palladium", "Silver", "Cadmium", "Indium", "Tin", "Antimony",
			"Tellurium", "Iodine", "Xenon", "Caesium", "Barium", "Lanthanum", "Cerium", "Praseodymium", "Neodymium",
			"Promethium", "Samarium", "Europium", "Gadolinium", "Terbium", "Dysprosium", "Holmium", "Erbium",
			"Thulium", "Ytterbium", "Lutetium", "Hafnium", "Tantalum", "Tungsten", "Rhenium", "Osmium", "Iridium",
			"Platinum", "Gold", "Mercury", "Thallium", "Lead", "Bismuth", "Polonium", "Astatine", "Radon", "Francium",
			"Radium", "Actinium", "Thorium", "Protactinium", "Uranium", "Neptunium", "Plutonium", "Americium",
			"Curium", "Berkelium", "Californium", "Einsteinium", "Fermium", "Mendelevium", "Nobelium", "Lawrencium",
			"Rutherfordium", "Dubnium", "Seaborgium", "Bohrium", "Hassium", "Meitnerium", "Darmstadtium",
			"Roentgenium", "Ununbium", "Ununtrium", "Ununquadium", "Ununpentium", "Ununhexium", "Ununseptium",
			"Ununoctium" };

	// Element Symbols Organized in Acending Atomic Number
	private final String[] ElementSymbols = { "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al",
			"Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga",
			"Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In",
			"Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho",
			"Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At",
			"Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr",
			"Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Uub", "Uut", "Uuq", "Uup", "Uuh", "Uus", "Uuo" };

	private String SearchArgs;
	final int Elements = 118;// number of elements in the periodic table
	// For reference only
	private String s; // Final return variable

	public PeriodicTableAcess(int searchParam) // search by atomic number
	{
		try
		{
			s = search(ElementNames[searchParam - 1]);
		}
		catch (SecurityException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NoSuchMethodException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public PeriodicTableAcess(String searchParam)// Search by Symbol or Name
	{
		SearchArgs = searchParam;
		try
		{
			try
			{
				s = Search();
			}
			catch (InstantiationException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		catch (SecurityException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NoSuchMethodException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// Main search engine to get the exact name of the element to avoid any
	// errors while reflecting
	private String Search() throws SecurityException, IllegalArgumentException, NoSuchMethodException,
			IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException
	{
		String elementName = "";
		if (SearchArgs.length() > 3)// search for name index
		{
			for (int i = 0; i < Elements; i++)
				if (SearchArgs.equalsIgnoreCase(ElementNames[i]))
					elementName = ElementNames[i];
		}
		else for (int i = 0; i < Elements; i++)
			// search for symbol index
			if (SearchArgs.equalsIgnoreCase(ElementSymbols[i]))
				elementName = ElementNames[i];
		return search(elementName);
	}

	// reflection method
	// Most sensitive method try not to modify
	private String search(String ElementName) throws SecurityException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException,
			InstantiationException
	{
		Class<?> c = Class.forName("PeriodicTable");// initialize the periodic
													// table class
		
		Method d = c.getMethod("get" + ElementName);// call the method get +
													// Element

		String ret = (String) d.invoke(c.newInstance());// get the info from
														// method
		return ret;
	}

	public void PrintSearch()// Print the search results to screen
	{
		System.out.println(s);
	}

	public String getSearchResult()// return the search result to be used by
								   // another method
	{
		return s;
	}
}
